// Written by HYD 2003/11/18

#define STOCK_DIR       "/tmp/stock"
#define KEY             1100
#define MATRIX          105
#define STOCKS        1900

#include <stdio.h>
#include <dirent.h>
#include "list.h"

void h_handled( char *filename );
void h_handlew( char *filename );
void h_handlem( char *filename );

typedef struct
{
  char stockno[8], trade_date[16], year[8], month[4],
    day[4], sunday[4], w_year[8],
    w_month[4], w_day[4], m_date[16], m_year[8], m_month[4], m_day[4];

  int total_deal_volume, w_total_deal_volume, reserve;

  float total_deal, begin, end, max,
    min, w_total_deal, w_begin, w_end,
    w_max, w_min, m_total_deal,
    m_total_deal_volume, m_begin, m_end, m_max, m_min;
}
stock;                          // 160B

stock stocks[STOCKS][MATRIX];   // 20.5MB
int no = 0;

int main(  )
{
  struct dirent *dirp;
  DIR *dp;
  char temp[128];

  if( ( shmid = shmget( KEY, sizeof( stocks ), 0 ) ) < 0 &&
      ( shmid = shmget( KEY, sizeof( stocks ), IPC_CREAT | 0666 ) ) < 0 )
  {
    perror( "shm" );
    return -1;
  }
  if( ( ltick = shmat( shmid, 0, 0 ) ) == ( void * ) -1 )
  {
    perror( "shmat" );
    return -2;
  }
  memset( stocks, 0, sizeof( stocks ) );
  if( ( dp = opendir( STOCK_DIR ) ) )
  {
    fprintf( stderr, "NOTICE reading directory %s files\n", STOCK_DIR );
    while( ( dirp = readdir( dp ) ) )
    {
      if( strcmp( dirp->d_name, "." ) && strcmp( dirp->d_name, ".." ) )
      {
        sprintf( temp, "%s/%s", STOCK_DIR, dirp->d_name );
        fprintf( stderr, "NOTICE handling file %s\n", temp );
        if( strlen( dirp->d_name ) > 6 )
        {
          if( !strncmp
              ( dirp->d_name + strlen( dirp->d_name ) - 6, "-d.txt", 6 ) )
          {
            h_handled( temp );
          }
          else
            if( !strncmp
                ( dirp->d_name + strlen( dirp->d_name ) - 6, "-w.txt", 6 ) )
          {
            h_handlew( temp );
          }
          else
            if( !strncmp
                ( dirp->d_name + strlen( dirp->d_name ) - 6, "-m.txt", 6 ) )
          {
            h_handlem( temp );
          }
        }
      }
    }
    closedir( dp );
    fprintf( stderr, "NOTICE finish reading directory %s files\n",
             STOCK_DIR );
  }
  else
  {
    fprintf( stderr, "ERROR can't open directory %s\n", STOCK_DIR );
  }
  shmdt( ( void * ) ltick );
  return 0;
}

void h_handled( char *filename )
{
  FILE *fd;
  int total = 0, newflag = 1, num, z;
  char line[256], *p, stockno[8];

  if( ( fd = fopen( filename, "r" ) ) )
  {
    strcpy( stockno, strtok( filename, "-" ) );
    for( z = 0; z < no; z++ )
    {
      if( !strcmp( stocks[no][0].stockno, stockno ) )
      {
        newflag = 0;
        break;
      }
    }
    if( newflag )
    {
      num = no;
    }
    else
    {
      num = z;
    }
    while( fgets( line, 256, fd ) )
    {
      if( !( p = strtok( line, "\t" ) ) )
      {
        break;
      }
      strcpy( stocks[num][total].stockno, stockno );
      strcpy( stocks[num][total].trade_date, p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      strcpy( stocks[num][total].year, p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      strcpy( stocks[num][total].month, p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      strcpy( stocks[num][total].day, p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].total_deal_volume = atoi( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].total_deal = ( float ) atof( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].begin = ( float ) atof( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].end = ( float ) atof( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].max = ( float ) atof( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].min = ( float ) atof( p );
    }
    fclose( fd );
    if( newflag )
    {
      no++;
    }
  }
  else
  {
    fprintf( stderr, "Warning: can't open file %s\n", filename );
  }
}

void h_handlew( char *filename )
{
  FILE *fd;
  int total = 0, newflag = 1, num, z;
  char line[256], *p, stockno[8];

  if( ( fd = fopen( filename, "r" ) ) )
  {
    strcpy( stockno, strtok( filename, "-" ) );
    for( z = 0; z < no; z++ )
    {
      if( !strcmp( stocks[no][0].stockno, stockno ) )
      {
        newflag = 0;
        break;
      }
    }
    if( newflag )
    {
      num = no;
    }
    else
    {
      num = z;
    }
    while( fgets( line, 256, fd ) )
    {
      if( !( p = strtok( line, "\t" ) ) )
      {
        break;
      }
      strcpy( stocks[num][total].stockno, stockno );
      strcpy( stocks[num][total].sunday, p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      strcpy( stocks[num][total].w_year, p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      strcpy( stocks[num][total].w_month, p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      strcpy( stocks[num][total].w_day, p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].w_total_deal_volume = atoi( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].w_total_deal = ( float ) atof( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].w_begin = ( float ) atof( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].w_end = ( float ) atof( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].w_max = ( float ) atof( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].w_min = ( float ) atof( p );
    }
    fclose( fd );
    if( newflag )
    {
      no++;
    }
  }
  else
  {
    fprintf( stderr, "Warning: can't open file %s\n", filename );
  }
}

void h_handlem( char *filename )
{
  FILE *fd;
  int total = 0, newflag = 1, num, z;
  char line[256], *p, stockno[8];

  if( ( fd = fopen( filename, "r" ) ) )
  {
    strcpy( stockno, strtok( filename, "-" ) );
    for( z = 0; z < no; z++ )
    {
      if( !strcmp( stocks[no][0].stockno, stockno ) )
      {
        newflag = 0;
        break;
      }
    }
    if( newflag )
    {
      num = no;
    }
    else
    {
      num = z;
    }
    while( fgets( line, 256, fd ) )
    {
      if( !( p = strtok( line, "\t" ) ) )
      {
        break;
      }
      strcpy( stocks[num][total].stockno, stockno );
      strcpy( stocks[num][total].m_date, p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      strcpy( stocks[num][total].m_year, p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      strcpy( stocks[num][total].m_month, p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      strcpy( stocks[num][total].m_day, p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].m_total_deal_volume = atoi( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].m_total_deal = ( float ) atof( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].m_begin = ( float ) atof( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].m_end = ( float ) atof( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].m_max = ( float ) atof( p );
      if( !( p = strtok( NULL, "\t" ) ) )
      {
        break;
      }
      stocks[num][total].m_min = ( float ) atof( p );
    }
    fclose( fd );
    if( newflag )
    {
      no++;
    }
  }
  else
  {
    fprintf( stderr, "Warning: can't open file %s\n", filename );
  }
}
